package com.example.androidmainshixun.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.AbstractWindowedCursor;
import android.database.Cursor;
import android.database.CursorWindow;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.androidmainshixun.database.DBHelper;

public class ZNBProvider extends ContentProvider {

    private static final String AUTHORITY = "com.jlastu.znb.provider";
    private static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final int MATCHER_GOODS = 1;
    private static final int MATCHER_GOODS_ID = 2;
    private static final int MATCHER_USER = 11;
    private static final int MATCHER_USER_ID = 12;
    private static final int MATCHER_PHOTO = 21;
    private DBHelper mDBHelper;

    static {//通配符：*所有字符，#所有数字
        // content://com.jlastu.znb.provider/goods
        uriMatcher.addURI(AUTHORITY, "goods", MATCHER_GOODS);
        // content://com.jlastu.znb.provider/goods/1
        uriMatcher.addURI(AUTHORITY, "goods/#", MATCHER_GOODS_ID);
        uriMatcher.addURI(AUTHORITY, "user", MATCHER_USER);
        uriMatcher.addURI(AUTHORITY, "user/#", MATCHER_USER_ID);
        uriMatcher.addURI(AUTHORITY, "photo", MATCHER_PHOTO);
    }

    @Override
    public boolean onCreate() {
        mDBHelper = new DBHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri,
                        @Nullable String[] projection,
                        @Nullable String selection,
                        @Nullable String[] selectionArgs,
                        @Nullable String sortOrder) {
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                return db.query("goods", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);
                return db.query("goods", projection, "_id = ?",
                        new String[]{String.valueOf(goods_id)}, null,
                        null, sortOrder, null);
            case MATCHER_USER:
                return db.query("user", projection, selection,
                        selectionArgs, null, null, sortOrder);
            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);
                Log.d("provider", "user_id ========= " + user_id);
                return db.query("user", projection, "_id = ?",
                        new String[]{String.valueOf(user_id)}, null,
                        null, sortOrder, null);
            case MATCHER_PHOTO:
                Cursor cursor = db.query("photo", projection, selection,
                        selectionArgs, null, null, sortOrder);
                CursorWindow cw = new CursorWindow("test", 5000000);
                AbstractWindowedCursor ac = (AbstractWindowedCursor) cursor;
                ac.setWindow(cw);
                return cursor;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        long insertId = -1;
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:
                insertId = db.insert("goods", null, values);
                break;
            case MATCHER_USER:
                insertId = db.insert("user", null, values);
                break;
            case MATCHER_PHOTO:
                insertId = db.insert("photo", null, values);
                break;
        }
        return ContentUris.withAppendedId(uri, insertId);
    }

    @Override
    public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
        for (ContentValues contentValues : values) {
            Log.d("", "bulkInsert ===== ");
            insert(uri, contentValues);
        }
        return super.bulkInsert(uri, values);
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:

            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);

            case MATCHER_USER:

            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);

        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case MATCHER_GOODS:

            case MATCHER_GOODS_ID:
                long goods_id = ContentUris.parseId(uri);

            case MATCHER_USER:

            case MATCHER_USER_ID:
                long user_id = ContentUris.parseId(uri);

        }
        return 0;
    }
}

